<?php 
class visitaController extends CController{
    public $model_name = 'venda/visita';

    public function __construct($vars) {
        parent::__construct($vars);
        $this->addToFreeCod('tele');
        $this->addToFreeCod('pendentes');
        $this->addToFreeCod('aprovadas');
        $this->addToFreeCod('recusadas');
    }
    
    public function nova(){
        if(isset($_SESSION['venda/empresa'])){
            unset($_SESSION['venda/empresa']);
            cookie::destroy ('visiting');
        }
        Redirect(LINK."/formulario");
    }
    
    public function formulario($display = true, $link = "") {
        $this->LoadClassFromPlugin('venda/visita/visitaVaziaController', 'vcont');
        $this->vcont->formulario($this->model);
        $this->setVars($this->vcont->getMessages());
        $this->display('');
    }
    
    public function edit($display = true, $link = "") {
        
        if($this->item['__venda_visita_bdstatus'] == 'unsaved'){
            EventTube::removeItemFromMenu('body-top', 'Voltar para Visita');
        }
        
        $this->LoadResource('html', 'html');
        $url = $this->html->getActionLinkIfHasPermission('venda/visita/nova', "Registre uma visita para uma nova empresa");
        $desc = "Temine de registrar a visita que está em progresso ou $url";
        EventTube::addEvent('body-top', "<div class='container-msg info'>$desc</div>");

        $this->genTags("Visitando a empresa ". end($this->item['venda_empresa_cod']));
        if(!empty($_POST)) cookie::destroy ('visiting');
        
        if($this->ChangeMenuAndSetAlerts()) parent::edit($display, $link);
        else $this->display ('');
    }
    
    public function show($display = true, $link = "") {
        $link = ($link == "")?LINK."/show":$link;
        $this->ChangeMenuAndSetAlerts();
        parent::show($display, $link);
    }
    
    public function apagar() {
        $this->prevent_redirect();
        if($this->ChangeMenuAndSetAlerts()) {
            parent::apagar();
            $status = $this->getVar('status');
            if($status == '1'){
                cookie::destroy('visiting');
                Redirect ('venda/empresa/show/'.$this->item['__venda_empresa_cod']);
            }
        }
        else $this->display ('');
    }

    private function ChangeMenuAndSetAlerts(){
        $this->LoadClassFromPlugin('venda/visita/visitaChecar', 'vc');
        $bool = $this->vc->checarVisita($this->item);
        $this->setVars($this->vc->getMessages());
        return $bool;
    }
    
    public function tele(){
        $this->filtrar('todas', "Todas as Visitas");
    }
    
    public function pendentes(){
        $this->filtrar('pendentes', "Visitas Pendentes");
    }
    
    public function aprovadas(){
        $this->filtrar('aprovou', "Visitas Aprovadas");
    }
    
    public function recusadas(){
        $this->filtrar('recusou', "Visitas Aprovadas");
    }
    
    private function filtrar($method, $title){
        $this->setPage();
        $item = $this->model->$method($this->page);
        if(!empty($item)){
            $this->genTags($title);
            $this->registerVar("title", $title);
        }
        $this->registerVar("item"        , $item);
        $this->registerVar("comp_action" , $this->getIndexListType());
        $this->registerVar("show_links"  , '');
    	$this->display(LINK . "/status");
    }
}
?>